package tool;


import java.io.*;
import java.util.Base64;

public class ImgUtils {

    //base64ת��
    //��תbase64

    // ͼƬת����base64�ַ���
    public static String GetImageStr() {
        String imgFile = "D:\\tanbing.jpg";// �������ͼƬ
        InputStream in = null;
        byte[] data = null;
        // ��ȡͼƬ�ֽ�����
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // ���ֽ�����Base64����
        Base64.Encoder encoder = Base64.getEncoder();
        System.out.println(encoder.encode(data));
        return encoder.encodeToString(data);// ����Base64��������ֽ������ַ���
    }

    // ���ֽ������ַ�������Base64���벢����ͼƬ
    public static boolean GenerateImage(String imgStr) {
        if (imgStr == null) {
            // ͼ������Ϊ��
            return false;
        }
        Base64.Decoder decoder = Base64.getDecoder();
        try {
            // Base64����
            byte[] b = decoder.decode(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// �����쳣����
                    b[i] += 256;
                }
            }
            // ����jpegͼƬ
            String imgFilePath = "D:\\tanbing2.jpg";// �����ɵ�ͼƬ
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
